package com.gjy.sql.oracle.newland.finance;

import com.gjy.sql.oracle.OracleUtil;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-07-15 10:41:04
 */
public class FinancialInit {

    public static InitData init() {
        InitData initData = new InitData();
        List<Map<String, String>> datas = new ArrayList<>();
        String sql = "SELECT * FROM SETTLE.COMMON_CHECK_FILE_DATA WHERE FINANCIAL_PERIOD = 202506 AND COST_FILE_NAME NOT LIKE 'NYJS_COST_ADJUST%'";
        try (Connection connection = OracleUtil.connection();
             PreparedStatement ps = connection.prepareStatement(sql);
             ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                Map<String, String> map = new HashMap<>();
                map.put("ORG_CODE", rs.getString("ORG_CODE"));
                map.put("ORG_NAME", rs.getString("ORG_NAME"));
                map.put("SUPPLY_NAME", rs.getString("SUPPLY_NAME"));
                map.put("CONTRACT_TITLE", rs.getString("CONTRACT_TITLE"));
                map.put("EXCL_TAX_SETTLE_FEE", rs.getString("AMOUNT"));
                map.put("TAX", rs.getString("TAX"));
                map.put("TAX_SETTLE_FEE", rs.getString("TAX_AMOUNT"));
                map.put("INSERT_TIME", rs.getString("INSERT_TIME"));
                map.put("COST_FILE_NAME", rs.getString("COST_FILE_NAME"));
                map.put("COST_CHK_FILE_NAME", rs.getString("COST_CHK_FILE_NAME"));
                map.put("COST_SUM_FILE_NAME", rs.getString("COST_SUM_FILE_NAME"));
                map.put("COST_SUM_CHECK_FILE_NAME", rs.getString("COST_SUM_CHECK_FILE_NAME"));
                map.put("DIGEST", rs.getString("DIGEST"));
                map.put("SUM_DIGEST", rs.getString("SUM_DIGEST"));
                datas.add(map);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        Map<String, List<Map<String, String>>> d1 = datas.stream().collect(Collectors.groupingBy(e -> e.get("COST_FILE_NAME")));
        List<DataSheet> dataSheetList = new ArrayList<>();
        d1.forEach((k, v) -> {
            DataSheet dataSheet = new DataSheet();
            dataSheet.setFileName(k);
            List<FinancialData> c1 = v.stream().map(e -> {
                FinancialData fd = new FinancialData();
                fd.setOrgCode(e.get("ORG_CODE"));
                fd.setOrgName(e.get("ORG_NAME"));
                fd.setSupplyName(e.get("SUPPLY_NAME"));
                fd.setContractTitle(e.get("CONTRACT_TITLE"));
                fd.setExclTaxSettleFee(e.get("EXCL_TAX_SETTLE_FEE"));
                fd.setTaxSettleFee(e.get("TAX_SETTLE_FEE"));
                fd.setTax(e.get("TAX"));
                return fd;
            }).collect(Collectors.toList());
            dataSheet.setContent(c1);
            dataSheetList.add(dataSheet);
        });
        initData.setDataSheetList(dataSheetList);

        Map<String, List<Map<String, String>>> d2 = datas.stream().collect(Collectors.groupingBy(e -> e.get("COST_CHK_FILE_NAME")));
        List<CheckSheet> checkSheetList = new ArrayList<>();
        d2.forEach((k, v) -> {
            CheckSheet checkSheet = new CheckSheet();
            checkSheet.setFileName(k);
            CheckData checkData = new CheckData();
            Map<String, String> map = v.get(0);
            checkData.setFileName(map.get("COST_FILE_NAME"));
            checkData.setFileTime(map.get("INSERT_TIME"));
            checkData.setDigest(map.get("DIGEST"));
            checkData.setCount(String.valueOf(v.size()));
            double exclTaxSettleFee = v.stream().mapToDouble(e -> new BigDecimal(e.get("EXCL_TAX_SETTLE_FEE")).doubleValue()).sum();
            checkData.setExclTaxSettleFee(String.valueOf(exclTaxSettleFee));
            double taxSettleFee = v.stream().mapToDouble(e -> new BigDecimal(e.get("TAX_SETTLE_FEE")).doubleValue()).sum();
            checkData.setTaxSettleFee(String.valueOf(taxSettleFee));
            double tax = v.stream().mapToDouble(e -> new BigDecimal(e.get("TAX")).doubleValue()).sum();
            checkData.setTax(String.valueOf(tax));
            checkSheet.setCheckData(checkData);
            checkSheetList.add(checkSheet);
        });
        initData.setCheckSheetList(checkSheetList);

        Map<String, List<Map<String, String>>> d3 = datas.stream().collect(Collectors.groupingBy(e -> e.get("COST_FILE_NAME")));
        SumSheet sumSheet = new SumSheet();
        sumSheet.setFileName(datas.get(0).get("COST_SUM_FILE_NAME"));
        List<SumData> sumDataList = new ArrayList<>();
        sumSheet.setSumDataList(sumDataList);
        d3.forEach((k, v) -> {
            SumData sumData = new SumData();
            sumData.setFileName(k);
            Map<String, String> map = v.get(0);
            sumData.setFileName(map.get("COST_FILE_NAME"));
            sumData.setFileTime(map.get("INSERT_TIME"));
            sumData.setDigest(map.get("SUM_DIGEST"));
            sumData.setCount(String.valueOf(v.size()));
            double exclTaxSettleFee = v.stream().mapToDouble(e -> new BigDecimal(e.get("EXCL_TAX_SETTLE_FEE")).doubleValue()).sum();
            sumData.setExclTaxSettleFee(String.valueOf(exclTaxSettleFee));
            double taxSettleFee = v.stream().mapToDouble(e -> new BigDecimal(e.get("TAX_SETTLE_FEE")).doubleValue()).sum();
            sumData.setTaxSettleFee(String.valueOf(taxSettleFee));
            double tax = v.stream().mapToDouble(e -> new BigDecimal(e.get("TAX")).doubleValue()).sum();
            sumData.setTax(String.valueOf(tax));
            sumDataList.add(sumData);
        });
        sumSheet.setSumDataList(sumDataList);
        initData.setSumSheet(sumSheet);

        Map<String, List<Map<String, String>>> d4 = datas.stream().collect(Collectors.groupingBy(e -> e.get("COST_SUM_FILE_NAME")));
        SumCheckSheet sumCheckSheet = new SumCheckSheet();
        sumCheckSheet.setFileName(datas.get(0).get("COST_SUM_CHECK_FILE_NAME"));
        d4.forEach((k, v) -> {
            SumCheckData sumCheckData = new SumCheckData();
            Map<String, String> map = v.get(0);
            sumCheckData.setFileName(map.get("COST_SUM_FILE_NAME"));
            sumCheckData.setFileTime(map.get("INSERT_TIME"));
            sumCheckData.setDigest(map.get("SUM_DIGEST"));
            sumCheckData.setCount(String.valueOf(v.size()));
            double exclTaxSettleFee = v.stream().mapToDouble(e -> new BigDecimal(e.get("EXCL_TAX_SETTLE_FEE")).doubleValue()).sum();
            sumCheckData.setExclTaxSettleFee(String.valueOf(exclTaxSettleFee));
            double taxSettleFee = v.stream().mapToDouble(e -> new BigDecimal(e.get("TAX_SETTLE_FEE")).doubleValue()).sum();
            sumCheckData.setTaxSettleFee(String.valueOf(taxSettleFee));
            double tax = v.stream().mapToDouble(e -> new BigDecimal(e.get("TAX")).doubleValue()).sum();
            sumCheckData.setTax(String.valueOf(tax));
            sumCheckSheet.setSumCheckData(sumCheckData);
        });
        initData.setSumCheckSheet(sumCheckSheet);
        return initData;
    }

}
